package 线程中断用法;

public class Demo implements Runnable {

    @Override
    public void run() {
        while (true) {
            if (Thread.currentThread().isInterrupted()) { // 要显示写出 “中断处理”
                System.out.println(Thread.currentThread().getName() + " 被中断");
                break; // 线程结束
            }else {
                System.out.println(Thread.currentThread().getName() + " 正常执行代码");
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Demo demo = new Demo();
        Thread thread1 = new Thread(demo);
        thread1.start();
        Thread.sleep(500);
        thread1.interrupt();    // 中断线程
    }
    //Thread-0 正常执行代码
    // .................
    //Thread-0 正常执行代码
    //Thread-0 被中断
}
